<template>
  <div class="register-container">
    <el-card class="register-box">
      <h2 class="register-title">用户注册</h2>
      <el-form :model="form" :rules="rules" ref="registerForm">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" @input="handleAccountInput"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading">注册</el-button>
          <el-button @click="$router.push('/login')">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<!-- src/components/Register.vue -->
<script>
import axios from '@/utils/axios'

export default {
  data() {
    const validateConfirm = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    return {
      loading: false,
      form: {
        account: '',
        username: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^\d+$/, message: '账号必须为数字', trigger: 'blur' },
          { min: 3, max: 16, message: '长度3-16位', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度2-10位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度6-20位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleAccountInput(value) {
      this.form.account = value.replace(/[^\d]/g, '')
    },
    submitForm() {
      this.$refs.registerForm.validate(valid => {
        if (!valid) return
        
        this.loading = true
        // 使用全路径调用注册接口
        axios.post('http://localhost:9083/api/user/register', {
          id: parseInt(this.form.account),
          username: this.form.username,
          password: this.form.password
        }).then(() => {
          this.$message.success('注册成功')
          this.$router.push('/login')
        }).catch(error => {
          this.$message.error(error.message || '注册失败')
        }).finally(() => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1e5799 0%,#207cca 100%);
}
.register-box {
  width: 500px;
  padding: 25px;
  border-radius: 5px;
}
</style>